package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.qo.PageResult;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequiresPermissions(value = {"department:list","部门列表"},logical = Logical.OR)
   @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.selctForPageResult(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }


    @RequiresPermissions(value = {"department:delete","部门删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
            if (id != null) {
                departmentService.delete(id);
            }
            return new JsonResult();
    }


    //贴自定义注解
    @RequiresPermissions(value = {"department:saveOrUpdate","部门添加/更新"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department dept) {
            if (dept.getId() != null) {
                departmentService.update(dept);
            } else {
                departmentService.save(dept);
            }
            return new JsonResult();
    }

}
